/**
 * Created by zhanglihe on 15/11/19.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'dialog',
    'model'
], function ($, common, ready, Collection, template, dialog, Model) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'amount/user_cahange_config',
                condition: {limit: 10, offset: 0},
                model: {
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-delete:click': 'del',
                    },
                    del: function (e) {
                        e.preventDefault();
                        var _this_item = this;
                        var data = {};
                        dialog({
                            content: '确定要删除?',
                            ok: function () {
                                data.id = _this_item.data.id;
                                data.act = 'del';
                                if (new Model({
                                        url: _global.url.api + 'amount/user_cahange_config',
                                        data: data,
                                    }).delete()) {
                                    _this.collection.reload();
                                    return true;
                                }
                                return false;
                            }, cancel: function () {
                            },
                        }).show(e.target);
                    },
                    edit: function (e) {
                        e.preventDefault();
                        var _this_item = this;
                        var data = {};
                        dialog({
                            title: '编辑折扣',
                            content: template('add-tpl', _this_item.data),
                            width: 600,
                            ok: function () {
                                data.id = _this_item.data.id;
                                data.amount = $("input[name='amount']").val();
                                data.discount_ratio = $("input[name='discount_ratio']").val();
                                if (new Model({
                                        url: _global.url.api + 'amount/user_cahange_config',
                                        data: data,
                                        valid_config: {
                                            amount: 'required',
                                            discount_ratio: 'required',
                                        },
                                    }).post()) {
                                    _this.collection.reload();
                                    return true;
                                }
                                return false;
                            }, cancel: function () {
                            },
                        }).show();
                    },
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                dialog({
                    title: '增加折扣',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        _this.data.amount = $("input[name='amount']").val();
                        _this.data.discount_ratio = $("input[name='discount_ratio']").val();
                        if (new Model({
                                url: _global.url.api + 'amount/user_cahange_config',
                                data: _this.data,
                                valid_config: {
                                    amount: 'required',
                                    discount_ratio: 'required',
                                },
                            }).post()) {
                            _this.collection.reload();
                            return true;
                        }
                        return false;
                    }, cancel: function () {
                    },
                }).show();
            });
        }
    };

    new index();

});